#pragma once
#ifndef skyImage_H__
#define skyImage_H__

#include "./skyAdapter.h"
#include "./skyObject.h"
#include "./skyFont.h"

namespace Sky 
{
	class Image : public Object
	{
	public:
		Image();
		~Image();

		typedef enum
		{
			FmtJpg = 0,
			FmtPng,
			FmtRawData,
		}EImageFormat;

		typedef enum
		{
			AlignCenter        = 0x33, ///< Horizontal center and vertical center.
			AlignTop           = 0x13, ///< Horizontal center and vertical top.
			AlignTopRight      = 0x12, ///< Horizontal right and vertical top.
			AlignRight         = 0x32, ///< Horizontal right and vertical center.
			AlignBottomRight   = 0x22, ///< Horizontal right and vertical bottom.
			AlignBottom        = 0x23, ///< Horizontal center and vertical bottom.
			AlignBottomLeft    = 0x21, ///< Horizontal left and vertical bottom.
			AlignLeft          = 0x31, ///< Horizontal left and vertical center.
			AlignTopLeft       = 0x11, ///< Horizontal left and vertical top.
		}ETextAlign;

		/** 
		@brief  Load the image from the specified path. 
		@param strPath   the absolute file path
		@param imageType the type of image, now only support tow types.
		@return  true if load correctly
		*/
		//bool createWithImageFile(String * strPath, EImageFormat imageType = FmtPng);

		/**
		@brief  Load image from stream buffer.

		@warning kFmtRawData only support RGBA8888
		@param pBuffer  stream buffer that hold the image data
		@param nLength  the length of data(managed in byte)
		@param nWidth, nHeight, nBitsPerComponent are used for kFmtRawData
		@return true if load correctly
		*/
		//bool createWithImageData(void * pData, 
		//					   int nDataLen, 
		//					   EImageFormat eFmt = FmtPng,
		//					   int nWidth = 0,
		//					   int nHeight = 0,
		//					   int nBitsPerComponent = 8);

		/**
		@brief	Create image with specified string.
		@param  pText       the text which the image show, nil cause init fail
		@param  nWidth      the image width, if 0, the width match the text's width
		@param  nHeight     the image height, if 0, the height match the text's height
		@param  eAlignMask  the test Alignment
		@param  pFontName   the name of the font which use to draw the text. If nil, use the default system font.
		@param  nSize       the font size, if 0, use the system default size.
		*/
		bool createWithString(
			 Char *    pText, 
			 Sky::Color color,
			 Sky::Font*	   pFont,
			ETextAlign      eAlignMask = AlignCenter);

		u8 *   getData()         { return m_pData; }
		int    getDataLen()            { return m_nWidth * m_nHeight; }

		bool hasAlpha()                     { return m_bHasAlpha; }
		bool isPremultipliedAlpha()         { return m_bPreMulti; }

		void release();

		/**
		@brief	Save the Image data to specified file with specified format.
		@param	pszFilePath		the file's absolute path, including file subfix
		@param	bIsToRGB		if the image is saved as RGB format
		*/
		bool saveToFile(const Char *pszFilePath, bool bIsToRGB = true);

		GetProperty(Width,	short,   m_nWidth);
		GetProperty(Height,short,   m_nHeight);
		GetProperty(BitsPerComponent,int, m_nBitsPerComponent);

	protected:
		/*bool _initWithJpgData(void *pData, int nDatalen);
		bool _initWithPngData(void *pData, int nDatalen);*/

		// @warning kFmtRawData only support RGBA8888
		bool _initWithRawData(void *pData, int nDatalen, int nWidth, int nHeight, int nBitsPerComponent);

		bool _saveImageToPNG(const Char *pszFilePath, bool bIsToRGB = true);
		bool _saveImageToJPG(const Char *pszFilePath);

		u8 *m_pData;
		bool m_bHasAlpha;
		bool m_bPreMulti;

	private:
		// noncopyable
		Image(const Image&    rImg);
		Image & operator=(const Image&);
	};

}
#endif // xResult_H__
